package com.goethe.translator;

import android.app.Activity;
import android.database.Cursor;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

public class GUIUtils{
	
	public final static int _ID = 0;
	public final static int GERMAN_WORD = 1;
	public final static int GERMAN_GENDER = 2;
	public final static int GERMAN_PLURAL = 3;
	public final static int SPANISH_WORD = 4;
	public final static int SPANISH_GENDER = 5;
	public final static int SPANISH_PLURAL = 6;
	public final static int WORD_TYPE = 7;
	public final static int WORD_LEVEL = 8;
	public final static int WORD_GROUP = 9;
	
	public void showUpdateResult(Activity activity, boolean success){
		if(success){
			activity.findViewById(R.id.translationsUpdated).setVisibility(android.view.View.VISIBLE);
			activity.findViewById(R.id.translationsNotUpdated).setVisibility(android.view.View.GONE);
		}else{
			activity.findViewById(R.id.translationsUpdated).setVisibility(android.view.View.GONE);
			activity.findViewById(R.id.translationsNotUpdated).setVisibility(android.view.View.VISIBLE);
		}
	}
	
	public void showWordNotFound(Activity activity){
        // We set visibility for the views
		activity.findViewById(R.id.connectionFailure).setVisibility(android.view.View.GONE);
		activity.findViewById(R.id.firstRow).setVisibility(android.view.View.GONE);
		activity.findViewById(R.id.firstSeparator).setVisibility(android.view.View.GONE);
		activity.findViewById(R.id.secondRow).setVisibility(android.view.View.GONE);
		activity.findViewById(R.id.secondSeparator).setVisibility(android.view.View.GONE);
		activity.findViewById(R.id.thirdRow).setVisibility(android.view.View.GONE);
		activity.findViewById(R.id.fourthRow).setVisibility(android.view.View.GONE);
		activity.findViewById(R.id.fifthRow).setVisibility(android.view.View.GONE);
		activity.findViewById(R.id.thirdSeparator).setVisibility(android.view.View.GONE);
		
		// We get an instance of the fade in animation and use it on the word not found message
		Animation fadeIn = AnimationUtils.loadAnimation(activity.getApplicationContext(), R.anim.fade_in);
		activity.findViewById(R.id.wordNotFound).setVisibility(android.view.View.VISIBLE);
		activity.findViewById(R.id.wordNotFound).startAnimation(fadeIn);
	}

	public void showTranslatorResults(Cursor queryResult, Activity activity){
      	// Set the first row - contains the languages
        TextView text = (TextView) activity.findViewById(R.id.translatorTableText1a);
        text.setText(activity.getString(R.string.spanish));
        text = (TextView) activity.findViewById(R.id.translatorTableText1b);
        text.setText(activity.getString(R.string.german));
        
      	// Set the second row - contains the actual words
        text = (TextView) activity.findViewById(R.id.translatorTableText2a);
        text.setText(queryResult.getString(SPANISH_GENDER) + " " + queryResult.getString(SPANISH_WORD) + " (" + queryResult.getString(SPANISH_PLURAL) + ")");
        text = (TextView) activity.findViewById(R.id.translatorTableText2b);
        text.setText(queryResult.getString(GERMAN_GENDER) + " " + queryResult.getString(GERMAN_WORD) + " (" + queryResult.getString(GERMAN_PLURAL) + ")");
      	
        // Set the third row - contains the word type
        text = (TextView) activity.findViewById(R.id.translatorTableText3a);
        text.setText(activity.getString(R.string.word_type) + ": ");
        text = (TextView) activity.findViewById(R.id.translatorTableText3b);
        text.setText(queryResult.getString(WORD_TYPE));
        
        // Set the fourth row - contains the word group
        text = (TextView) activity.findViewById(R.id.translatorTableText4a);
        text.setText(activity.getString(R.string.word_group) + ": ");
        text = (TextView) activity.findViewById(R.id.translatorTableText4b);
        text.setText(queryResult.getString(WORD_GROUP));
        
        // Set the fifth row - contains the word level
        text = (TextView) activity.findViewById(R.id.translatorTableText5a);
        text.setText(activity.getString(R.string.word_level) + ": ");
        text = (TextView) activity.findViewById(R.id.translatorTableText5b);
        text.setText(queryResult.getString(WORD_LEVEL));
        
        // We get an instance of the fade in animation
		Animation fadeIn = AnimationUtils.loadAnimation(activity.getApplicationContext(), R.anim.fade_in);
		
    	// We set visibility for the views
		activity.findViewById(R.id.connectionFailure).setVisibility(android.view.View.GONE);
		activity.findViewById(R.id.wordNotFound).setVisibility(android.view.View.GONE);
		activity.findViewById(R.id.firstRow).setVisibility(android.view.View.VISIBLE);
		activity.findViewById(R.id.firstRow).startAnimation(fadeIn);
		activity.findViewById(R.id.firstSeparator).setVisibility(android.view.View.VISIBLE);
		activity.findViewById(R.id.firstSeparator).startAnimation(fadeIn);
		activity.findViewById(R.id.secondRow).setVisibility(android.view.View.VISIBLE);
		activity.findViewById(R.id.secondRow).startAnimation(fadeIn);
		activity.findViewById(R.id.secondSeparator).setVisibility(android.view.View.VISIBLE);
		activity.findViewById(R.id.secondSeparator).startAnimation(fadeIn);
		activity.findViewById(R.id.thirdRow).setVisibility(android.view.View.VISIBLE);
		activity.findViewById(R.id.thirdRow).startAnimation(fadeIn);
		activity.findViewById(R.id.fourthRow).setVisibility(android.view.View.VISIBLE);
		activity.findViewById(R.id.fourthRow).startAnimation(fadeIn);
		activity.findViewById(R.id.fifthRow).setVisibility(android.view.View.VISIBLE);
		activity.findViewById(R.id.fifthRow).startAnimation(fadeIn);
		activity.findViewById(R.id.thirdSeparator).setVisibility(android.view.View.VISIBLE);
		activity.findViewById(R.id.thirdSeparator).startAnimation(fadeIn);
	}
	
	public void showDieDerDasWord(Cursor queryResult, Activity activity){
        // We get an instance of the fade in animation
		Animation fadeIn = AnimationUtils.loadAnimation(activity.getApplicationContext(), R.anim.fade_in);
		
		// We set the buttons visibility
		activity.findViewById(R.id.buttonsNextFinish).setVisibility(android.view.View.GONE);
		activity.findViewById(R.id.buttonsDieDerDas).setVisibility(android.view.View.VISIBLE);
		activity.findViewById(R.id.buttonsDieDerDas).startAnimation(fadeIn);
		
        if((queryResult == null) || !(queryResult.getCount()>0)){
	        // We set visibility for the views
        	activity.findViewById(R.id.correct_message).setVisibility(android.view.View.GONE);
        	activity.findViewById(R.id.incorrect_message).setVisibility(android.view.View.GONE);
        	activity.findViewById(R.id.random_word).setVisibility(android.view.View.GONE);
        	activity.findViewById(R.id.random_word_translation).setVisibility(android.view.View.GONE);
        	activity.findViewById(R.id.connectionFailure).setVisibility(android.view.View.VISIBLE);
        	activity.findViewById(R.id.connectionFailure).startAnimation(fadeIn);
        }else{
        	// We move to the first result
	        queryResult.moveToFirst();
	        
	        // We load the random word
	        TextView text = (TextView) activity.findViewById(R.id.random_word);
	        text.setText(queryResult.getString(GERMAN_WORD));
	        
	        // We load the random word translation
	        text = (TextView) activity.findViewById(R.id.random_word_translation);
	        text.setText("(" + queryResult.getString(SPANISH_WORD) + ")");
	        
	        // We set visibility for the views
	        activity.findViewById(R.id.connectionFailure).setVisibility(android.view.View.GONE);
	        activity.findViewById(R.id.correct_message).setVisibility(android.view.View.GONE);
	        activity.findViewById(R.id.incorrect_message).setVisibility(android.view.View.GONE);
	        activity.findViewById(R.id.random_word).setVisibility(android.view.View.VISIBLE);
	        activity.findViewById(R.id.random_word).startAnimation(fadeIn);
	        activity.findViewById(R.id.random_word_translation).setVisibility(android.view.View.VISIBLE);
	        activity.findViewById(R.id.random_word_translation).startAnimation(fadeIn);
        }
	}
	
	public void showDieDerDasResults(int counterCorrect, int counterIncorrect, Activity activity){
        // We get an instance of the fade in animation
		Animation fadeIn = AnimationUtils.loadAnimation(activity.getApplicationContext(), R.anim.fade_in);
		
        // We concatenate the results to the proper views
        TextView text = (TextView) activity.findViewById(R.id.total_correct_message);
        text.setText(activity.getString(R.string.total_correct) + " " + counterCorrect);
        text = (TextView) activity.findViewById(R.id.total_incorrect_message);
        text.setText(activity.getString(R.string.total_incorrect) + " " + counterIncorrect);
        
        // We set visibility for the views
		activity.findViewById(R.id.connectionFailure).setVisibility(android.view.View.GONE);
		activity.findViewById(R.id.correct_message).setVisibility(android.view.View.GONE);
		activity.findViewById(R.id.incorrect_message).setVisibility(android.view.View.GONE);
		activity.findViewById(R.id.random_word).setVisibility(android.view.View.GONE);
		activity.findViewById(R.id.random_word_translation).setVisibility(android.view.View.GONE);
		activity.findViewById(R.id.buttonsDieDerDas).setVisibility(android.view.View.GONE);
		activity.findViewById(R.id.buttonsNextFinish).setVisibility(android.view.View.GONE);
		activity.findViewById(R.id.connectionFailure).setVisibility(android.view.View.GONE);
		activity.findViewById(R.id.total_correct_message).setVisibility(android.view.View.VISIBLE);
		activity.findViewById(R.id.total_correct_message).startAnimation(fadeIn);
		activity.findViewById(R.id.total_incorrect_message).setVisibility(android.view.View.VISIBLE);
		activity.findViewById(R.id.total_incorrect_message).startAnimation(fadeIn);
	}
	
	public void showDieDerDasCorrect(Activity activity){
        // We get an instance of the fade in animation
		Animation fadeIn = AnimationUtils.loadAnimation(activity.getApplicationContext(), R.anim.fade_in);
		
        // We set visibility for the views
		activity.findViewById(R.id.incorrect_message).setVisibility(android.view.View.GONE);
		activity.findViewById(R.id.correct_message).setVisibility(android.view.View.VISIBLE);
		activity.findViewById(R.id.correct_message).startAnimation(fadeIn);
	}
	
	public void showDieDerDasIncorrect(Activity activity){
        // We get an instance of the fade in animation
		Animation fadeIn = AnimationUtils.loadAnimation(activity.getApplicationContext(), R.anim.fade_in);
		
        // We set visibility for the views
		activity.findViewById(R.id.correct_message).setVisibility(android.view.View.GONE);
		activity.findViewById(R.id.incorrect_message).setVisibility(android.view.View.VISIBLE);
		activity.findViewById(R.id.incorrect_message).startAnimation(fadeIn);
	}
	
	public void showDieDerDasSelectOption(Cursor queryResult, Activity activity){
        // We get an instance of the fade in animation
		Animation fadeIn = AnimationUtils.loadAnimation(activity.getApplicationContext(), R.anim.fade_in);
		
        // We set the correction message
        TextView text = (TextView) activity.findViewById(R.id.random_word);
        text.setText(queryResult.getString(GERMAN_GENDER) + " " + queryResult.getString(GERMAN_WORD));
        text.startAnimation(fadeIn);
    	
		// We set the buttons visibility
        activity.findViewById(R.id.buttonsDieDerDas).setVisibility(android.view.View.GONE);
        activity.findViewById(R.id.buttonsNextFinish).setVisibility(android.view.View.VISIBLE);
        activity.findViewById(R.id.buttonsNextFinish).startAnimation(fadeIn);
	}
}
